package com.canxi.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.canxi.common.core.domain.PageQuery;
import com.canxi.common.core.page.TableDataInfo;
import com.canxi.common.utils.StringUtils;
import com.canxi.cxcommon.utils.PageUtils;
import com.canxi.system.service.ISysConfigService;
import com.canxi.user.domain.CxUser;
import com.canxi.user.domain.bo.CxUserBo;
import com.canxi.user.domain.vo.CxUserVo;
import com.canxi.user.mapper.CxUserMapper;
import com.canxi.user.service.IUserService;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-22
 */
@RequiredArgsConstructor
@Service
public class UserServiceImpl  extends JoinServiceImpl<CxUserMapper, CxUser>  implements IUserService {

    private final CxUserMapper baseMapper;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询用户信息
     */
    @Override
    public CxUserVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public CxUserVo queryByPhone(String phone) {
        JoinLambdaWrapper<CxUser> wrapper = joinLambdaQueryWrapper(CxUser.class);
        wrapper.eq(CxUser::getPhone, phone);
        return baseMapper.selectVoOne(wrapper);
    }

    @Override
    public CxUserVo getByOpenid(String openid) {
        JoinLambdaWrapper<CxUser> wrapper = joinLambdaQueryWrapper(CxUser.class);
        wrapper.eq(CxUser::getOpenid,openid);
        CxUserVo userVo = baseMapper.selectVoOne(wrapper);
        return userVo;
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public TableDataInfo<CxUserVo> queryPageList(CxUserBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<CxUser> lqw = buildQueryWrapper(bo);
//        Page<CxUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        return TableDataInfo.build(result);

        return PageUtils.buildDataInfo(this.baseMapper.queryCxUserList(PageUtils.buildPage(), BeanUtil.toBean(bo, CxUserVo.class)));
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public List<CxUserVo> queryList(CxUserBo bo) {
        LambdaQueryWrapper<CxUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CxUser> buildQueryWrapper(CxUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CxUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPid() != null, CxUser::getPid, bo.getPid());
        lqw.eq(StringUtils.isNotBlank(bo.getUnionid()), CxUser::getUnionid, bo.getUnionid());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenid()), CxUser::getOpenid, bo.getOpenid());
        lqw.eq(StringUtils.isNotBlank(bo.getUtype()), CxUser::getUtype, bo.getUtype());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), CxUser::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), CxUser::getPassword, bo.getPassword());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), CxUser::getNickname, bo.getNickname());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), CxUser::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), CxUser::getAvatar, bo.getAvatar());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), CxUser::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), CxUser::getAddress, bo.getAddress());
        lqw.eq(bo.getCredits() != null, CxUser::getCredits, bo.getCredits());
        lqw.eq(bo.getTickets() != null, CxUser::getTickets, bo.getTickets());
        lqw.eq(bo.getBalance() != null, CxUser::getBalance, bo.getBalance());
        lqw.eq(StringUtils.isNotBlank(bo.getIdentity()), CxUser::getIdentity, bo.getIdentity());
        lqw.eq(bo.getLastLoginTime() != null, CxUser::getLastLoginTime, bo.getLastLoginTime());
        if(ObjectUtil.isNotNull(bo.getParams().get("searchValue"))){
            lqw.likeLeft(CxUser::getNickname, bo.getParams().get("searchValue").toString()).or().likeRight(CxUser::getNickname, bo.getParams().get("searchValue").toString()).or().likeLeft(CxUser::getPhone, bo.getParams().get("searchValue").toString()).or().likeRight(CxUser::getPhone, bo.getParams().get("searchValue").toString());

        }


        return lqw;
    }

    /**
     * 新增用户信息
     */
    @Override
    public Boolean insertByBo(CxUserBo bo) {
        CxUser add = BeanUtil.toBean(bo, CxUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户信息
     */
    @Override
    public Boolean updateByBo(CxUserBo bo) {
        CxUser update = BeanUtil.toBean(bo, CxUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CxUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public String queryPasswordById(String id) {
        String s = this.baseMapper.queryUserPasswordById(id);
        return s;
    }

    @Override
    public int resetPasswordById(String id) {

        String s = configService.selectConfigByKey("cx_default_password");

        if(StrUtil.isBlank(s)){
            s = "123456";
        }
        s = BCrypt.hashpw(s);

        int update = this.baseMapper.update(new LambdaUpdateWrapper<CxUser>().eq(CxUser::getId, id).set(CxUser::getPassword, s));

        return update;
    }


}
